package com.duowei.diandan.bean;

/**
 * 会员拥有的券信息ims_card_members_coupon
 */
public class ImsCardMembersCoupon {
    /** 主键 */
    public int id;
    /**
     * 一个微信公众号一个weid
     * 关联 ims_wechats的weid
     */
    public int weid;
    /** 优惠券ID,管理ims_card_coupon的id */
    public int couponId;
    /**
     * 用户的唯一身份ID
     * 关联ims_card_members的 from_user
     */
    public String fromUser;
    /** 1为正常状态，2为已使用 */
    public int status;
    /** 收银员 */
    public String receiver;
    /** 使用时间 */
    public int consumeTime;
    /** 领券时间 */
    public int createTime;
    /** 券有效期开始 */
    public int startTime;
    /** 券有效期止 */
    public int endTime;
    /** Uid会员唯一标示对应ims_fans中的ID */
    public int uid;

    /**
     * 电子券使用状态更新
     *
     * @param weid
     * @param fromUser
     * @param couponId  优惠券ID
     * @param receiver  收银员
     * @param usedTime  使用时间
     * @param dealId
     * @param couponNum 使用券的数量
     * @return
     */
    public static String toUpdateString(int weid, String fromUser, int couponId, String receiver, String usedTime, String dealId, int couponNum) {
        return "update ims_card_members_coupon" +
                " set status = 2, receiver = '" + receiver + "', consumetime = UNIX_TIMESTAMP('" + usedTime + "'), deal_id = '" + dealId + "'" +
                "where weid = " + weid + " and couponid = " + couponId + " and from_user = '" + fromUser + "' and status = 1 and (" +
                "  current_date() between CAST(FROM_UNIXTIME(starttime, '%Y-%m-%d') as datetime) and CAST(" +
                "      FROM_UNIXTIME(endtime, '%Y-%m-%d') as datetime) or IFNULL(starttime, 0) = 0 or IFNULL(endtime, 0) = 0)" +
                "order by CAST(FROM_UNIXTIME((case IFNULL(endtime, 0)" +
                "                             when 0" +
                "                               then 1893427200" +
                "                             else endtime end), '%Y-%m-%d') as datetime)" +
                "limit " + couponNum + "|";
    }
}
